//
//  TT_JumpManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/5/29.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TTUDIDManager.h"

#import "GoldAgentInfoRequest.h"

#import "TT_JumpManager.h"
#import "ViewControllerPreRender.h"

#import "StoreCategoryViewController.h"
#import "StoreDetailViewController.h"
#import "PackageDetailViewController.h"
#import "ServiceCategoryViewController.h"
#import "CouponListViewController.h"
#import "TT_WebviewController.h"
#import "StrictElectionListViewController.h"
#import "StoreHotelDetailViewController.h"
#import "ServiceCatSubSegViewController.h"
#import "PortfilioDetailViewController.h"
#import "ProductDetailViewController.h"
#import "PlaformMsgViewController.h"
#import "PlatformMsgDetailViewController.h"
#import "MarryToolViewController.h"
#import "TopicListViewController.h"
#import "PostListViewController.h"
#import "TopicDetailViewController.h"
#import "PostDetailViewController.h"

#import "E_invitationSegViewController.h"
#import "SearchViewController.h"
#import "AgentApplyViewController.h"
#import "RingApplyViewController.h"
#import "JLActDetailViewController.h"
#import "WeddingCarCartViewController.h"

#import "HotTopicSubSegViewController.h"


@implementation TT_JumpManager

+ (TT_JumpManager *)sharedManager {
    static TT_JumpManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_JumpManager new];
    });
    return shareInstance;
}

- (NSDictionary *)convert2DictionaryWithJSONString:(NSString *)jsonString{
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"%@",err);
        return nil;
    }
    return dic;
}

- (void)jumpWithModel:(HomeADItemEventModel *)jumpModel completion:(TT_JumpManagerCallBack)completion {
    TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
    switch (jumpModel.eventClickId) {
        case TT_JUMPTYPE_StoreList_Simple: {
            StoreCategoryViewController *vc = [[StoreCategoryViewController alloc] init];
            vc.className = jumpModel.sellerName;
            vc.classId = [jumpModel.eventSellerId integerValue];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_StoreList:{
            StoreCategoryViewController *vc = [[StoreCategoryViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_StoreDetail:{

            StoreDetailViewController *vc = [[StoreDetailViewController alloc] init];
            vc.storeId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
            
        } break;
        case TT_JUMPTYPE_PackageDetail:{

            if ([jumpModel.classId isEqualToString:Store_Category_Car]) {//对婚车套餐进行新老区分
                
                if (jumpModel.goodsVersion == 2) {
                    WeddingCarDetailViewController *vc = [[WeddingCarDetailViewController alloc] init];
                    vc.packageId = jumpModel.eventSellerId;
                    [navController pushViewController:vc animated:YES];
                } else {
                    PackageDetailViewController *vc = [[PackageDetailViewController alloc] init];
                    vc.packageId = jumpModel.eventSellerId;
                    [navController pushViewController:vc animated:YES];
                }
                
            } else {
                PackageDetailViewController *vc = [[PackageDetailViewController alloc] init];
                vc.packageId = jumpModel.eventSellerId;
                [navController pushViewController:vc animated:YES];
            }

            if (completion) {
                completion(YES, @"");
            }
            
        } break;
        case TT_JUMPTYPE_Category:{

            NSString *scId = jumpModel.eventSellerId;
            if (scId.length > 0) {
                ServiceCatSubSegViewController *vc = [[ServiceCatSubSegViewController alloc] init];
                NSInteger scatId = scId.integerValue;
                vc.scId = scatId;
                [navController pushViewController:vc animated:YES];

            } else {
                ServiceCategoryViewController *vc = [[ServiceCategoryViewController alloc] init];
                [navController pushViewController:vc animated:YES];
            }
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_CouponList:{

            CouponListViewController *vc = [[CouponListViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }

        } break;
        case TT_JUMPTYPE_NewH5:
        case TT_JUMPTYPE_H5:{
            NSString *url = @"";
            if ([jumpModel.eventSellerId isKindOfClass:[NSString class]]) {
                if ([jumpModel.eventSellerId containsString:@"?"]) {
                    url = [NSString stringWithFormat:@"%@&cityId=%@&cityName=%@&userId=%@&longitude=%lf&latitude=%lf&deviceUniqueNo=%@",jumpModel.eventSellerId,[TTUserModel sharedObject].cityId,[Utilities stringToUnicode:[TTUserModel sharedObject].cityName],[TTUserModel sharedObject].userId,[TTUserModel sharedObject].longitude,[TTUserModel sharedObject].latitude,[TTUDIDManager getUniqueDeviceID]];
                } else {
                    url = [NSString stringWithFormat:@"%@?cityId=%@&cityName=%@&userId=%@&longitude=%lf&latitude=%lf&deviceUniqueNo=%@",jumpModel.eventSellerId,[TTUserModel sharedObject].cityId,[Utilities stringToUnicode:[TTUserModel sharedObject].cityName],[TTUserModel sharedObject].userId,[TTUserModel sharedObject].longitude,[TTUserModel sharedObject].latitude,[TTUDIDManager getUniqueDeviceID]];
                }
            } else {
                url = @"";
            }
            TT_WebviewController *webVC = [[TT_WebviewController alloc] init];
            webVC.webUrl = url;
            [navController pushViewController:webVC animated:YES];
            if (completion) {
                completion(YES, @"");
            }
//            [CATransaction commit];
//            [[ViewControllerPreRender defaultRender] showRenderedViewController:TT_BaseViewController.class completion:^(UIViewController * _Nonnull vc) {
//            }];
        } break;
        case TT_JUMPTYPE_YXStore:{

            StrictElectionListViewController *vc = [[StrictElectionListViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_Hall:{

            StoreHotelDetailViewController *vc = [[StoreHotelDetailViewController alloc] init];
            vc.storeId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_SerCategoryAll:{

            ServiceCategoryViewController *vc = [[ServiceCategoryViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_ThemeSubDetail:{

            HotTopicSubSegViewController *vc = [[HotTopicSubSegViewController alloc] init];
            vc.classId = jumpModel.classId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
            
        case TT_JUMPTYPE_H5Back:{

            [SVProgressHUD showSuccessWithStatus:@"操作成功"];
            [SVProgressHUD dismissWithDelay:1];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [navController popViewControllerAnimated:NO];
                if (completion) {
                    completion(YES, @"");
                }
            });
            
        } break;
        case TT_JUMPTYPE_CaseDetail:{

            PortfilioDetailViewController *vc = [[PortfilioDetailViewController alloc] init];
            vc.soId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_IM_TCS:{

            [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_tongchoushi withQuickReply:jumpModel.eventSellerId];
        } break;
        case TT_JUMPTYPE_ProductDetail:{
            ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
            vc.goodsId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_GoodsDetail:{
            ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
            vc.goodsId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_SysNoti:{

            PlatformMsgDetailViewController *vc = [[PlatformMsgDetailViewController alloc] init];
            vc.snId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_MarryTool:{

            MarryToolViewController *vc = [[MarryToolViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_TopicList:{

            TopicListViewController *vc = [[TopicListViewController alloc] init];
            vc.scId = [jumpModel.eventSellerId integerValue];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_E_Invitation:{

            E_invitationSegViewController *vc = [[E_invitationSegViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_Search:{

            SearchViewController *vc = [[SearchViewController alloc] init];
            vc.keyWord = jumpModel.iconName;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_JoinAgent:{

            [GoldAgentInfoRequest request_goldAgentInfoWith:jumpModel.eventSellerId comption:^(BOOL success, BA_AgentApplyType applyType, CommonGoldAgentModel * _Nonnull agentModel) {
               if (success) {
                   switch (applyType) {
                       case BA_AgentApplyType_applyed:{
                           if (completion) {
                               completion(NO, @"您已申请推荐官,请勿重复提交");
                           }
                       } break;
                       case BA_AgentApplyType_none:{
                           
                           AgentApplyViewController *vc = [[AgentApplyViewController alloc] init];
                           vc.agentModel = agentModel;
                           [navController pushViewController:vc animated:YES];
                           if (completion) {
                               completion(YES, @"");
                           }
                       } break;
                       default:
                           break;
                   }
               }
            }];
        } break;
        case TT_JUMPTYPE_ApplyRing:{

            RingApplyViewController *vc = [[RingApplyViewController alloc] init];
            vc.seller = [[CommonSellerModule alloc] init];
            vc.seller.sellerName = jumpModel.sellerName;
            vc.seller.sellerId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_PostList:{
            PostListViewController *vc = [[PostListViewController alloc] init];
            vc.tid = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_TopicDetail:{
            TopicDetailViewController *vc = [[TopicDetailViewController alloc] init];
            vc.pid = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_PostDetail:{
            PostDetailViewController *vc = [[PostDetailViewController alloc] init];
            vc.pid = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
        } break;
        case TT_JUMPTYPE_JL_ACT:{
            
            JLActDetailViewController *vc = [[JLActDetailViewController alloc] init];
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
            
        } break;
        case TT_JUMPTYPE_CarQueue:{
            
            WeddingCarCartViewController *vc = [[WeddingCarCartViewController alloc] init];
            vc.storeId = jumpModel.eventSellerId;
            [navController pushViewController:vc animated:YES];
            if (completion) {
                completion(YES, @"");
            }
            
        } break;
        default:{
            //[SVProgressHUD showWithStatus:@"未定义的事件类型，请检查并升级app版本"];
            //[SVProgressHUD dismissWithDelay:1];
            NSLog(@"未定义的事件类型，请检查并升级app版本");
            if (completion) {
                completion(YES, @"");
            }
        } break;
    }
}

- (void)jumpWithDetailModel:(CommonPackageModel *)model completion:(TT_JumpManagerCallBack)completion {
    
    TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;

    if ([model.classId isEqualToString:Store_Category_Car]) {//对婚车套餐进行新老区分
        
        if (model.goodsVersion == 2) {
            WeddingCarDetailViewController *vc = [[WeddingCarDetailViewController alloc] init];
            vc.packageId = model.goodsId;
            [navController pushViewController:vc animated:YES];
        } else {
            PackageDetailViewController *vc = [[PackageDetailViewController alloc] init];
            vc.packageId = model.goodsId;
            [navController pushViewController:vc animated:YES];
        }
        
    } else {
        PackageDetailViewController *vc = [[PackageDetailViewController alloc] init];
        vc.packageId = model.goodsId;
        [navController pushViewController:vc animated:YES];
    }
    
    
}

@end
